/*
   Copyright (c) 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2012-04-09 Marco Riccardi - initial implementation
 *	2015-01-11 Marco Riccardi - canceled type list
 *
 */
package org.mizartools.dli;

public class ReductionRegistration extends ItemDefinition {

	private Loci loci = null;
    private Term term1 = null;
    private Term term2 = null;
	
	private ReductionRegistration(){
		super(null);
	}

	public ReductionRegistration(Loci loci, Term term1, Term term2) throws DliException {
				super(null);
		if (loci == null || term1 == null || term2 == null){
			throw new DliException();
		}
		this.loci = loci;
		this.term1 = term1;
		this.term2 = term2;
    }

	public static ItemDefinition newInstance(String string) throws DliParseException {
		ReductionRegistration reductionRegistration = new ReductionRegistration();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in ReductionRegistration");
		}
		if (!string.substring(0, position).equals("redreg")) {
			throw new DliParseException("["+string+"] is not valid in ReductionRegistration");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in ReductionRegistration");
		}
		if (string.startsWith("$loci")){
			reductionRegistration.loci = Loci.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1);
		} else {
			throw new DliParseException("["+string+"] is not valid in ReductionRegistration");
		}
		positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in ReductionRegistration");
		}
		reductionRegistration.term1 = Term.newInstance(string.substring(0, positionComma));
		string = string.substring(positionComma+1);
		reductionRegistration.term2 = Term.newInstance(string);
		return reductionRegistration;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("redreg(");
		sb.append(loci.toString());
		sb.append(",");
		sb.append(term1.toString());
		sb.append(",");
		sb.append(term2.toString());
		sb.append(")");
		return sb.toString();
		
	}
	
}
